package com.atguigu.gmall.item.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/item")
public class TestController2 {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @GetMapping("/test2")
    public void testLock() {
        //  上锁
        //  setnx key value;
        //  Boolean result = this.redisTemplate.opsForValue().setIfAbsent("lock", "ok");
        //  set lock ok ex 10 nx
        //  将固定值，变为动态值！
        String uuid = UUID.randomUUID().toString();
        Boolean result = this.redisTemplate.opsForValue().setIfAbsent("lock1", uuid, 3, TimeUnit.SECONDS);
        System.out.println("huoqujieguo:"+result);
    }
}
